package com.winning.sx.microframework.task;

import com.winning.sx.microframework.plugins.wx.WxConfig;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


@Component
public class GetAccessTokenTask {

	private static final Logger log = Logger.getLogger(GetAccessTokenTask.class);
	
	/**
	 * 2个小时刷一次
	 */
//	@Scheduled(fixedDelay = 7100000)
	public void flushToken(){
		log.info("刷新 accessToken");
		String accessToken = null;
		String ticket=null;
		String expiresIn = null;
		String appid = WxConfig.appid;
		String appsecret = WxConfig.appSecret;
		String url = "https://api.weixin.qq.com/cgi-bin/token?"
				+ "grant_type=client_credential&appid=" + appid
				+ "&secret=" + appsecret;
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet
					.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			System.out.println("message====" + message);
			JSONObject demoJson = JSONObject.fromObject(message);
			accessToken = demoJson.getString("access_token");
			expiresIn = demoJson.getString("expires_in") + "000"; // 转换为毫秒
			System.out.println("accessToken====" + accessToken);
			System.out.println("expiresIn===" + expiresIn);
			WxConfig.accessToken = accessToken;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
//		https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
		url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?"
				+ "access_token=" + accessToken
				+ "&type=jsapi";
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet
					.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			JSONObject demoJson = JSONObject.fromObject(message);
			ticket = demoJson.getString("ticket");
			System.out.println("ticket====" + ticket);
			WxConfig.jsapiTicket = ticket;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 2个小时刷一次
	 */
//	@Scheduled(fixedDelay = 1100000)
	public void flushToken2(){
		log.info("刷新 accessToken");
		String accessToken = null;
		String ticket=null;
		String expiresIn = null;
		String appid = WxConfig.appid;
		String appsecret = WxConfig.appSecret;
		String url = "http://a.sxsrmyy.com:6019/WebChat/sweepCode/getAccessToken.action";
		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet
					.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			http.setDoOutput(true);
			http.setDoInput(true);
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			String data[]=message.split("\\|");
			WxConfig.accessToken = data[0];
			WxConfig.jsapiTicket=data[1];
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}